package com.example.hewei.smarttransport.Dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.example.hewei.smarttransport.R;
import com.example.hewei.smarttransport.Request.BaseRequest;
import com.example.hewei.smarttransport.Request.BaseRequest.OnResponseEventListener;
import com.example.hewei.smarttransport.Request.BaseRequest.RequestResult;
import com.example.hewei.smarttransport.Request.RegisterRequest;


/**
 * Created by 何伟 on 2017/3/7.
 */

public class RegisterDialog extends BaseDialog {
    private EditText mUserNameET;
    private EditText mPasswordET;
    private Button mCloseBtn;
    private Button mConfirmBtn;
    private RegisterRequest mRequest;

    public RegisterDialog() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.dialog_register, container);
    }

    @Override
    public void onActivityCreated(Bundle arg0) {
        super.onActivityCreated(arg0);
        setRequest();
        initView();
        setListener();
    }

    /**
     * 设置请求对象
     *
     */
    private void setRequest() {
        mRequest = new RegisterRequest("");
        mRequest.setOnResponseEventListener(new OnResponseEventListener() {

            @Override
            public void onResponse(BaseRequest request, RequestResult result) {
                if (mRequest.isSuccess()) {
                    showAlertDialog(mContext.getString(R.string.prompt),
                            mContext.getString(R.string.register_success));
                    RegisterDialog.this.dismiss();
                } else {
                    showAlertDialog(mContext.getString(R.string.prompt),
                            mContext.getString(R.string.register_failed));
                }
            }
        });
    }

    /**
     * 初始化控件
     */
    private void initView() {
        mCloseBtn = (Button) getView().findViewById(R.id.close);
        mUserNameET = (EditText) getView().findViewById(R.id.userName);
        mPasswordET = (EditText) getView().findViewById(R.id.password);
        mConfirmBtn = (Button) getView().findViewById(R.id.confirm);
    }

    /**
     * 设置监听器
     */
    private void setListener() {
        mCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterDialog.this.dismiss();
            }
        });
        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userRegisterEnter();
            }
        });
    }

    /**
     * 用户注册方法
     */
    private void userRegisterEnter() {
        String userName = mUserNameET.getText().toString().trim();
        String password = mPasswordET.getText().toString().trim();
        if (userName.equals("")) {
            showAlertDialog(mContext.getString(R.string.prompt),
                    mContext.getString(R.string.username_not_empty));
            return;
        }
        if (password.equals("")) {
            showAlertDialog(mContext.getString(R.string.prompt),
                    mContext.getString(R.string.password_not_empty));
            return;
        }
        mRequest.setUserName(userName);
        mRequest.setPassword(password);
        startRequest(mRequest);
    }
}
